/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#pragma once
#include <list>

#include "IRender.h"

#include "../shared/vector.h"
#include "../shared/Vector3f.h"
#include "../shared/color.h"

float NextRand();
/**
=======================
A particle
=======================
*/
struct Particle
{
    Particle() { Reset(); };
    ~Particle() {};

    // position
    Vector2f    position;
    // acceleration
    Vector2f    acceleration;
    // velocity
    Vector2f    velocity;
    // color
    Vector3f    color;
    // alpha value
    float       alpha;
    // life left
    float       lifeTime;
    // fade time
    float       fadeTime;
    // status
    bool        isDead;
    // nextAlpha Update
    float       nextAlphaUpdate;
    // particle size
    float       size;
	// rotation angle
	float		angle;


    void Reset() {
        position.ZeroOut();
        acceleration.ZeroOut();
        velocity.ZeroOut();
        color.ZeroOut();
        lifeTime = 0.0f;
        fadeTime = 0.0f;

        nextAlphaUpdate = 0.0f;
        size    = 0.0f;
        angle   = 0.0f;
        isDead   = false;
        
    };
};


/**
========================
A source for particles to spew out of
========================
*/
class Emitter
{
public:
    Emitter(void);

    /** Set the id */
    void SetID( unsigned int id ) { m_id = id; };
    /** Get the id */
    unsigned int GetID() { return m_id; };
    /** Update the Emitters */
    virtual void Update(long dt);

    /** Render the Emitters */
    virtual void Render(IRender* r, ICamera* cam);

    /** Reset all Vars */
    void Reset();
    /* Spawn Rate */
    float   SpawnRate;
    /* Emition Angle Span */
    float   MaxAngle;
    /* Max Particles at a time */
    float   MaxParticles;
    /* Max Total particles allowed for the total emitter */
    float   MaxTotalParticles;
    /** Particle Size */
    float   ParticleSize;
    /** Life Span of particles */
    float   LifeSpan;
    /** Life span of emitter */
    float   LifeSpanOfEmitter;

    /** Jitter Factor */
    float   Jitter;
    /** Wander Radius */
    float   WanderRadius;
    /** Wander Distance */
    float   WanderDistance;

    /* Start Color */
    Vector3f   StartColor;
    /* End Color */
    Vector3f   EndColor;
    /* Color fade time */
    float      ColorFadeTime;
    /* Start alpha */
    float       StartAlpha;
    /* Ending Alpha */
    float       EndAlpha;
    /* Alpha Fade Time */
    float       AlphaFadeTime;

    /* Initial Velocity */
    Vector2f InitVel;
    /** Position of the emitter */
    Vector2f Position;
    /** Accel */
    Vector2f    Acceleration;
    /** Friction */
    float   Friction;

protected:
    /** Calculate Jitter Factor */
    void CalculateJitter( Particle &p, Vector2f &vel );
    /** Spawn a particle */
    virtual void Spawn();
    /** Clear dead particles */
    void ClearDead();
    // alpha fade incrementer
    float   m_alphaFade;
    // spawn timer
    float   m_spawnTimer;
    // number of particles created
    int     m_particlesCreated;
    // id
    unsigned int m_id;
    // particle list
    typedef std::list<Particle> type_Particles;
    type_Particles  m_particles;

    // assign ids
    static  unsigned int m_emitterIDs;
public:
    virtual ~Emitter(void);
};
